﻿using ConfigInfoWebPlatform.API.BusinessCommon;
using ConfigInfoWebPlatform.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConfigInfoWebPlatform.API.DBCommon
{
    /// <summary>
    /// 版本数据层
    /// </summary>
    public class DAL_Version
    {
        private IFreeSql freeSql;
        /// <summary>
        /// 版本数据层
        /// </summary>
        public DAL_Version()
        {
            freeSql = FreesqlFactory.GetFreeSql("configplatform");
        }

        /// <summary>
        /// 获取版本列表数据
        /// </summary>
        public List<VersionModel> GetVersionList(string did,bool isAdmin)
        {
            var query = freeSql.Select<ConfigVersion>()
                             .WhereIf(!isAdmin, x => x.DId == did || x.Id == "3950678987046510592")
                             .ToList(x=>new VersionModel { 
                                Id=x.Id,
                                Did=x.DId,
                                Name=x.Name,
                                Catalogue=x.Catalogue,
                                CreateTime=x.CreateTime,
                                Remark=x.Remark
                             });

            return query;
        }

        /// <summary>
        /// 获取版本详情数据
        /// </summary>
        public VersionModel GetVersionDetail(string id)
        {
            var query = freeSql.Select<ConfigVersion>()
                             .Where(x => x.Id == id)
                             .ToOne(x => new VersionModel
                             {
                                 Id = x.Id,
                                 Did = x.DId,
                                 Name = x.Name,
                                 Catalogue = x.Catalogue,
                                 Remark = x.Remark,
                                 CreateTime = x.CreateTime
                             });
            return query;
        }

        /// <summary>
        /// 查看版本名称是否存在
        /// </summary>
        /// <param name="name">版本名称</param>
        /// <param name="id">版本Id</param>
        /// <returns></returns>
        public bool IsExists(string name, string id)
        {
            var result = freeSql.Select<ConfigVersion>()
                              .Where(x => x.Name == name)
                              .WhereIf(!string.IsNullOrWhiteSpace(id), x => x.Id != id)
                              .Count();

            return result > 0 ? true : false;
        }

        /// <summary>
        /// 添加版本信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int InsertVersion(VersionModel model)
        {
            ConfigVersion version = new ConfigVersion();
            version.Id= Snowflake.Instance().GetId().ToString();
            version.Name = model.Name;
            version.DId = model.Did;
            version.Catalogue = model.Catalogue;
            version.CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            version.Remark = model.Remark;
            var result = freeSql.Insert<ConfigVersion>(version).ExecuteAffrows();
            return result;
        }

        /// <summary>
        /// 修改版本信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int EditVersion(VersionModel model)
        {
            var result = freeSql.Update<ConfigVersion>()
                              .Set(x => x.Name, model.Name)
                              .Set(x => x.Remark, model.Remark)
                              .Where(x => x.Id == model.Id)
                              .ExecuteAffrows();
            return result;
        }

        /// <summary>
        /// 查看版本信息是否存在
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool IsExists(string id)
        {
            var result = freeSql.Select<ConfigVersion>()
                              .Where(x => x.Id == id)
                              .Count();
            return result > 0 ? true : false;
        }

        /// <summary>
        /// 删除版本信息
        /// </summary>
        public int DeleteVersion(string id)
        {
            var result = freeSql.Delete<ConfigVersion>()
                              .Where(x => x.Id == id)
                              .ExecuteAffrows();
            return result;
        }
    }
}
